# LOAD NECESSARY PACKAGES ------------------------------------------------------

library(pacman)

# tidyverse
p_load(dplyr, tidyr, stringr, forcats, purrr, ggplot2, patchwork, broom, readr, 
       tidylog)

# analysis
p_load(naniar, did, modelsummary, estimatr)

# misc
p_load(here, kableExtra)


# LOAD DATA --------------------------------------------------------------------

nga_nfeoutcomes <- read_csv(
  here("Manuscript", "Tables", "Annex", "nga_nfeoutcomes.csv"))

nga_empoutcomes <- read_csv(
  here("Manuscript", "Tables", "Annex", "nga_empoutcomes.csv"))

eth_nfeoutcomes <- read_csv(
  here("Manuscript", "Tables", "Annex", "eth_nfeoutcomes.csv"))

eth_empoutcomes <- read_csv(
  here("Manuscript", "Tables", "Annex", "eth_empoutcomes.csv"))

# Define consistent outcomes variables and labels

hh_nfeoutcomes <- c("nfe_tot", "nfe_retail", "nfe_manuf",
                    "nfe_wmntot", "nfe_wmnretail", "nfe_wmnmanuf")

hh_nfeoutcomes_labels <- c("NFE All", "NFE Retail", "NFE Manuf.",
                           "NFE All (Wmn)", "NFE Retail (Wmn)", "NFE Manuf. (Wmn)")

indiv_empoutcomes <- c("emp", "emp_nonfarm", "emp_farm")

indiv_empoutcomes_labels <- c("Emp. All", "Emp. Non-farm", "Emp. Farm")

# VISUALISATION ----------------------------------------------------------------

nga_nfeoutcomes <- nga_nfeoutcomes %>% 
  select(treatment, model, group, estimate, conf.low, conf.high, covs) %>% 
  filter(covs %in% c("DRDiD - vill & hh covs.")) %>% 
  filter(group == "Average", model %in% hh_nfeoutcomes) %>% 
  mutate(model = factor(model, levels = c(hh_nfeoutcomes), 
                        labels = c(hh_nfeoutcomes_labels)),
         treatment = factor(treatment, levels = c("Community", "Household"))) %>%
  ggplot(aes(x = model, y = estimate, ymin = conf.low, ymax = conf.high, colour = treatment)) +
  geom_point(position = position_dodge(width = 0.4), size = 2) +
  geom_errorbar(position = position_dodge(width = 0.4), width = 0.2) +
  geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) +
  scale_color_discrete(direction = -1) +
  coord_cartesian(ylim = c(-0.2,0.2)) +
  scale_y_continuous(breaks = scales::pretty_breaks()) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  labs(x = NULL,
       y = "ATT (pp. change in likelihood of NFE)",
       shape = "Level of treatment",
       colour = "Level of treatment",
       subtitle = "Nigeria (2010-2015)")

eth_nfeoutcomes <- eth_nfeoutcomes %>% 
  select(treatment, model, group, estimate, conf.low, conf.high, covs) %>% 
  filter(covs %in% c("DRDiD - vill & hh covs.")) %>% 
  filter(group == "Average", model %in% hh_nfeoutcomes) %>% 
  mutate(model = factor(model, levels = c(hh_nfeoutcomes), 
                        labels = c(hh_nfeoutcomes_labels)),
         treatment = factor(treatment, levels = c("Community", "Household"))) %>%
  ggplot(aes(x = model, y = estimate, ymin = conf.low, ymax = conf.high, colour = treatment)) +
  geom_point(position = position_dodge(width = 0.4), size = 2) +
  geom_errorbar(position = position_dodge(width = 0.4), width = 0.2) +
  geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) +
  scale_color_discrete(direction = -1, drop = F) +
  coord_cartesian(ylim = c(-0.2,0.2)) +
  scale_y_continuous(breaks = scales::pretty_breaks()) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  labs(x = NULL,
       y = "ATT (pp. change in likelihood of NFE)",
       shape = "Level of treatment",
       colour = "Level of treatment",
       subtitle = "Ethiopia (2011-2015)")

wrap_plots(nga_nfeoutcomes, eth_nfeoutcomes, ncol = 2) + 
  plot_layout(guides = "collect") &
  theme(legend.position = "bottom")

ggsave(here("Manuscript", "Figures", "nfedid.png"), width = 8, height = 4)

nga_empoutcomes <- nga_empoutcomes %>% 
  select(treatment, model, group, estimate, conf.low, conf.high, covs) %>%  
  filter(covs %in% c("DRDiD - vill & indiv. covs.")) %>% 
  filter(group == "Average") %>% 
  mutate(model = factor(model, levels = indiv_empoutcomes, labels = indiv_empoutcomes_labels),
         treatment = factor(treatment, levels = c("Community", "Household"))) %>%
  ggplot(aes(x = model, y = estimate, ymin = conf.low, ymax = conf.high, colour = treatment)) +
  geom_point(position = position_dodge(width = 0.4), size = 2) +
  geom_errorbar(position = position_dodge(width = 0.4), width = 0.2) +
  geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) +
  scale_color_discrete(direction = -1) +
  coord_cartesian(ylim = c(-0.2,0.2)) +
  scale_y_continuous(breaks = scales::pretty_breaks()) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  labs(x = NULL,
       y = "ATT (pp. change in employment likelihood)",
       shape = "Level of treatment",
       colour = "Level of treatment",
       subtitle = "Nigeria (2010-2015)")

eth_empoutcomes <- eth_empoutcomes %>% 
  select(treatment, model, group, estimate, conf.low, conf.high, covs) %>%  
  filter(covs %in% c("DRDiD - vill & indiv. covs.")) %>% 
  filter(group == "Average") %>% 
  mutate(model = factor(model, levels = indiv_empoutcomes, labels = indiv_empoutcomes_labels),
         treatment = factor(treatment, levels = c("Community", "Household"))) %>%
  ggplot(aes(x = model, y = estimate, ymin = conf.low, ymax = conf.high, colour = treatment)) +
  geom_point(position = position_dodge(width = 0.4), size = 2) +
  geom_errorbar(position = position_dodge(width = 0.4), width = 0.2) +
  geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) +
  scale_color_discrete(direction = -1) +
  coord_cartesian(ylim = c(-0.2,0.2)) +
  scale_y_continuous(breaks = scales::pretty_breaks()) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  guides(colour = "none") +
  labs(x = NULL,
       y = "ATT (pp. change in employment likelihood)",
       shape = "Level of treatment",
       colour = "Level of treatment",
       subtitle = "Ethiopia (2011-2015)")

wrap_plots(nga_empoutcomes,eth_empoutcomes, ncol = 2) + 
  plot_layout(guides = "collect") &
  theme(legend.position = "bottom")

ggsave(here("Manuscript", "Figures", "empdid.png"), width = 8, height = 4)

